//
//  DeliveredResumeTableViewCell.m
//  CYWFrame
//
//  Created by 于顺燊 on 2018/9/24.
//  Copyright © 2018年 Macx. All rights reserved.
//

#import "DeliveredResumeTableViewCell.h"

@implementation DeliveredResumeTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
	self.rightBtn1.layer.cornerRadius = 12;
	self.rightBtn2.layer.cornerRadius = 12;
	self.rightBtn1.backgroundColor = UIMainColorRGB;
	self.rightBtn2.backgroundColor = UIMainColorRGB;
}

- (void)setInfo:(NSDictionary *)info
{
	if (_info != info) {
		_info = info;
		self.name.text = info[@"name"];
		if (![NSString isEmpty:self.name.text]) {
			NSMutableAttributedString *userNameAttri = [[NSMutableAttributedString alloc] initWithString:info[@"name"]];
			NSTextAttachment *sexAttch = [[NSTextAttachment alloc] init];
			if ([info[@"sex"] isEqualToString:@"1"]) {
				sexAttch.image = IMAGE(@"icon_sex_boy");
			}
			else if([info[@"sex"] isEqualToString:@"2"])
			{
				sexAttch.image = IMAGE(@"icon_sex_girl");
			}
			sexAttch.bounds = CGRectMake(0, -3, 15, 15);
			NSAttributedString *sexString = [NSAttributedString attributedStringWithAttachment:sexAttch];
			[userNameAttri insertAttributedString:sexString atIndex:self.name.text.length];
			self.name.attributedText = userNameAttri;
		}
		
		self.position.text = info[@"category_title"];
		if (![NSString isEmpty:info[@"head_img"]]) {
			[self.photo sd_setImageWithURL:[NSURL URLWithString:info[@"head_img"]] placeholderImage:IMAGE(@"placeHolder")];
		}else {
			self.photo.image = IMAGE(@"placeHolder");
		}
		self.workExp.text = info[@"work_life"];
		self.salary.text = info[@"salary"];
		self.education.text = info[@"education"];
		self.deliverTime.text = [NSString stringWithFormat:@"邀请时间:%@",info[@"create_time"]];
		NSString *stateStr;
		if (self.type == 0) {
			switch ([info[@"status"] integerValue]) {
				case 0:
					stateStr = @"待查看";
					break;
				case 1:
					stateStr = @"已查看";
					break;
				case 2:
					stateStr = @"已同意";
					break;
				case 3:
					stateStr = @"已拒绝";
					break;
				default:
					break;
			}
		}
		else {
			switch ([info[@"status"] integerValue]) {
				case 0:
					stateStr = @"待查看";
					break;
				case 1:
					stateStr = @"已查看";
					break;
				case 2:
					stateStr = @"已邀请";
					break;
				case 3:
					stateStr = @"已拒绝";
					break;
				default:
					break;
			}
		}
		self.state.text = stateStr;

		if (self.type == 1) {
			self.deliverTime.text = [NSString stringWithFormat:@"投递时间:%@",info[@"create_time"]];
			if ([info[@"status"] integerValue] != 3 && [info[@"status"] integerValue] != 2) {
				self.rightBtn1.hidden = NO;
				self.rightBtn2.hidden = NO;
				[self.rightBtn2 addTarget:self action:@selector(rejectInvitation:) forControlEvents:UIControlEventTouchUpInside];
				[self.rightBtn1 addTarget:self action:@selector(chooseDate:) forControlEvents:UIControlEventTouchUpInside];
			}
			else
			{
				self.rightBtn1.hidden = YES;
				self.rightBtn2.hidden = YES;
			}
		}
	}
}

- (void)rejectInvitation:(UIButton *)sender
{
	[self.delegate1 rejectInvitation:self.info[@"delivery_id"]];
}

- (void)chooseDate:(UIButton *)sender
{
	[self.delegate1 chooseDateForDeliveryId:self.info[@"delivery_id"]];
}

- (void)setFrame:(CGRect)frame
{
	frame.origin.y += 10;
	
	frame.size.height -= 10;
	
	[super setFrame:frame];
}

-(void)setModel:(InterviewManageModel *)model
{
	if (_model != model) {
		_model = model;
		self.name.text = model.name;
		NSMutableAttributedString *userNameAttri = [[NSMutableAttributedString alloc] initWithString:model.name];
		NSTextAttachment *sexAttch = [[NSTextAttachment alloc] init];
		if ([model.sex isEqualToString:@"1"]) {
			sexAttch.image = IMAGE(@"icon_sex_boy");
		}
		else if([model.sex isEqualToString:@"2"])
		{
			sexAttch.image = IMAGE(@"icon_sex_girl");
		}
		sexAttch.bounds = CGRectMake(0, -3, 15, 15);
		NSAttributedString *sexString = [NSAttributedString attributedStringWithAttachment:sexAttch];
		[userNameAttri insertAttributedString:sexString atIndex:self.name.text.length];
		self.name.attributedText = userNameAttri;
		
		self.position.text = model.category_title;
		[self.photo sd_setImageWithURL:[NSURL URLWithString:model.head_img] placeholderImage:IMAGE(@"placeHolder")];
		self.workExp.text = model.work_life;
		self.salary.text = model.salary;
		self.education.text = model.education;
		//self.deliverTime.text = [NSString stringWithFormat:@"邀请时间:%@",model.create_time];
		self.deliverTime.hidden = YES;
		self.interviewTime.hidden = NO;
		self.interviewTime.text = [NSString stringWithFormat:@"面试时间:%@", [self changeTimeStamptoString:model.interview_time]];
		self.rightBtn1.hidden = YES;
		self.rightBtn2.hidden = YES;
		NSString *stateStr;
		if (self.type == 2)
		{
			switch ([model.result integerValue]) {
				case 0:
					stateStr = @"未回复";
					self.rightBtn2.hidden = NO;
					self.rightBtn1.hidden = NO;
					[self.rightBtn2 setTitle:@"合适" forState:UIControlStateNormal];
					[self.rightBtn1 setTitle:@"不合适" forState:UIControlStateNormal];
					[self.rightBtn1 setTitleColor:kGrayColor forState:UIControlStateNormal];
					[self.rightBtn1 setBackgroundColor:UISeparatedColorRGB];
					[self.rightBtn2 addTarget:self action:@selector(employ:) forControlEvents:UIControlEventTouchUpInside];
					[self.rightBtn1 addTarget:self action:@selector(notEmploy:) forControlEvents:UIControlEventTouchUpInside];
					break;
				case 1:
					stateStr = @"已同意";
					break;
				case 2:
					stateStr = @"已发offer";
					self.state.adjustsFontSizeToFitWidth = YES;
					break;
				case -1:
					stateStr = @"已拒绝";
					break;
				default:
					break;
			}
		}
		else if(self.type == 3)
		{
			switch ([model.result integerValue]) {
				case 0:
					stateStr = @"未回复";
					break;
				case 1:
					stateStr = @"已同意";
					break;
				case -1:
					stateStr = @"已拒绝";
					break;
				default:
					break;
			}
		}
		else
		{
			switch ([model.is_confirmed integerValue]) {
				case 0:
					stateStr = @"已选择";
					self.remarkL.hidden = NO;
					self.remark.hidden = NO;
					self.remark.text = model.remark;
					break;
				case 1:
					stateStr = @"已确认";
					self.remarkL.hidden = NO;
					self.remark.hidden = NO;
					self.remark.text = model.remark;
					break;
				case 2:
					stateStr = @"未选择";
					self.rightBtn2.hidden = NO;
					[self.rightBtn2 setTitle:@"选择时间" forState:UIControlStateNormal];
					[self.rightBtn2 addTarget:self action:@selector(chooseDate2:) forControlEvents:UIControlEventTouchUpInside];
					self.remarkL.hidden = NO;
					self.remark.hidden = NO;
					self.remark.text = model.remark;
					break;
				default:
					break;
			}
		}
		if (self.remark.text.length == 0) {
			self.remark.text = @"暂无备注";
		}
		else {
			self.remark.text = [model.remark stringByAppendingString:@"\n"];
		}
		self.state.text = stateStr;
	}
}

- (NSString *)changeTimeStamptoString:(NSString *)timeStampString {

	// iOS 生成的时间戳是10位
	NSTimeInterval interval = [timeStampString doubleValue];
	
	if (interval == 0) {
		return @"";
	}
	NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
	NSString *dateString = [formatter stringFromDate: date];
	NSLog(@"服务器返回的时间戳对应的时间是:%@",dateString);
	
	return dateString;
}

- (void)chooseDate2:(UIButton *)sender
{
	NSLog(@"the info is %@,%@", self.model.name, self.model.delivery_id);
	[self.delegate2 chooseDateForInterviewId:self.model.interview_id];
}

- (void)employ:(UIButton *)sender
{
	[self.delegate2 employ:self.model.interview_id];
}

- (void)notEmploy:(UIButton *)sender
{
	[self.delegate2 notEmploy:self.model.interview_id];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
